package com.xia.controller;

import com.xia.dao.article.IArticleDao;
import com.xia.dao.article.IVideoDao;
import com.xia.domian.*;
import com.xia.service.ICommentService;
import com.xia.util.common.Result;
import com.xia.dao.article.ICommunityDao;
import com.xia.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 文章控制器
 */
@CrossOrigin
@RestController
@RequestMapping("/article")
public class ArticleController {
    private IArticleService articleService;
    private ICommentService commentService;
    private ICommunityDao communityDao;
    private IArticleDao articleDao;
    private IVideoDao videoDao;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Autowired
    public void setCommentService(ICommentService commentService) {
        this.commentService = commentService;
    }
    @Autowired
    public void setVideoDao(IVideoDao videoDao) {
        this.videoDao = videoDao;
    }
    @Autowired
    public void setArticleDao(IArticleDao articleDao) {
        this.articleDao = articleDao;
    }
    @Autowired
    public void setCommunityDao(ICommunityDao communityDao) {
        this.communityDao = communityDao;
    }
    @Autowired
    public void setArticleService(IArticleService articleService) {
        this.articleService = articleService;
    }

    /**
     * 查看指定视频
     */
    @GetMapping("/getVideoOnId/{videoId}")
    public Result getVideoOnId(@PathVariable Integer videoId){
        Map<String, Object> commentMap=commentService.selectArticleComment(videoId,2);
        Result result=Result.success();
        if (commentMap.containsKey("error")){
            return Result.error("此评论数量不够");
        }
        result.put("commentMap",commentMap);
        result.put("videoData",videoDao.selectVideoOnId(videoId));
        return result;
    }

    /**
     * 查询视频
     */
    @GetMapping("/getVideo/{index}")
    public Result getVideo(@PathVariable Integer index){
        return Result.success(videoDao.selectVideo(index));
    }

    /**
     * 发布视频
     */
    @PostMapping("/submitVideo")
    public void submitVideo(@RequestBody Video video){
        articleService.issueVideo(video);
    }
    /**
     * 更改用户对文章点赞状态
     */
    @PostMapping("/usersLikeArticle")
    public void usersUpdateArticleLike(@RequestBody LikeArticle likeArticle)throws IOException {
        articleService.likeArticleOperation(likeArticle);
    }
    /**
     * 删除文章
     */
    @DeleteMapping("/deleteArticle/{articleId}")
    public Result deleteArticle(@PathVariable Integer articleId){
        articleDao.deleteArticle(articleId);
        return Result.success();
    }

    /**
     * 文章更新
     */
    @PostMapping("/updateArticle")
    public Result updateArticleMsg(@RequestBody Article article){
        articleDao.updateArticle(article);
        return Result.success();
    }

    /**
     * 根据文章ID查询文章放置编辑
     */
    @GetMapping("/getCompileArticle/{articleId}")
    public Result getCompileArticle(@PathVariable Integer articleId){
        return Result.success(articleDao.selectArticleContentId(articleId));
    }

    /**
     * 创作文章/帖子
     * @param article 前端传入的创作信息
     */
    @PostMapping("/write")
    public Result WriteArticle(@RequestBody Article article){
        articleService.writeArticle(article);
        return Result.success("发布成功");
    }

    /**
     * 查询社区列表
     */
    @GetMapping("/initCommunity")
    public Result initCommunity(){
        List<Community> communityList=communityDao.selectAllCommunity();
        return Result.success(communityList);
    }

    /**
     * 初始化时发送数据
     */
    @GetMapping("/initArticle")
    public Result initArticle(){
        return Result.success(articleService.articlesUtil(0,4));
    }

    /**
     * 初始化时发送轮播图
     */
    @GetMapping("/initSlideShow")
    public Result initSlideShow(){
        //查询轮播图数据
        return Result.success(articleService.articlesUtil(0,4));
    }

    /**
     * 根据社区ID推送文章
     * @param communityId 社区ID
     */
    @GetMapping("/getPostList/{communityId}")
    public Result getPostList(@PathVariable Integer communityId){
        return Result.success(articleService.selectArticleSizeUtil(1, 0, communityId, 0));
    }

    /**
     * 推送文章内容
     */
    @GetMapping("/getArticleContent")
    public Result getArticleContent(){
        return Result.success(articleService.articlesUtil(0,4));
    }

    /**
     * 搜索文章/帖子
     * @param searchContent 搜索内容
     */
    @GetMapping("/searchLikeArticle/{searchContent}")
    public Result searchLikeArticle(@PathVariable String searchContent){
        Map<String,Object> map=articleService.searchArticle(searchContent);
        return Result.success(map);
    }

    /**
     * 用户的文章/帖子信息
     * @param usersId 请求用户
     */
    @GetMapping("/selectUserMsgArticle/{usersId}")
    public Result selectUserMsgArticle(@PathVariable Integer usersId){
     return Result.success(articleService.selectMsgArticle(usersId));
    }

    /**
     * 获取热议数据
     */
    @GetMapping("/initArticleCommentList")
    public Result initArticleCommentList(){
        ListOperations<String, Object> listOps = redisTemplate.opsForList();
        List<Object> lists = listOps.range("hotList", 0, 0);
        assert lists != null;
        ArrayList<Object> articleList = new ArrayList<>(lists);
        return Result.success(articleList);
    }
}
